#include <QtGui/QApplication>
#include <QDebug>
#include "def.h"
#include "mconfig.h"
#include "method.h"
#include "mwin.h"

#include <QDir>
#include <QSplashScreen>

int main(int argc, char ** argv){
    // command line parameters
    // test string from mac 22
    QString cfgfile="";
    for(int i = 0; i<=argc; i++){
        if(QString(argv[i])=="-h" or
           QString(argv[i])=="--help" or
           QString(argv[i])=="-?"){
            printf(cli_use);
            exit(0);
        }
        if(QString(argv[i])=="-v" or
           QString(argv[i])=="--version"){
            printf("%s version %s.%s build %s\n%s"
                   ,app_name,app_major,app_minor,app_build,author);
            exit(0);

        }
        if(QString(argv[i])=="-c" or
           QString(argv[i])=="--config"){
            cfgfile=argv[i+1];
        }
    }
    QApplication a(argc, argv);
    //load config
    MConfig cfg;
    if(cfgfile==""){
    cfgfile=QString().append(QCoreApplication::applicationDirPath()).append("/qtester.cfg");
    }
    if(!cfg.load(cfgfile)){
        // make default config.
        // fixit - show error message, and configure dialog
        qDebug()<<"Error load config <"<<cfgfile<<">";
        exit(1);
    }
    // load methods
    qDebug()<<cfg.methods_path;
    qDebug()<<"search methods";
    qDebug()<<"test methods dir";
    QDir dir(cfg.methods_path);
    QList<Method*> mdl; // list of methods

    //QPixmap pixmap("sp.jpg");
    //QSplashScreen * splash = new QSplashScreen(pixmap);

    //splash->showMessage("LOAD");
    //splash->showFullScreen();
    //splash->show();

    if(dir.exists()){
        qDebug()<<"DIR YES";
        //search all method.* dirs
        QStringList filters;
        filters << "method.*";
        QFileInfoList ldr=dir.entryInfoList(filters, QDir::NoDotAndDotDot | QDir::Dirs,NULL);
        qDebug()<<ldr.size();
        for(int i = 0 ; i < ldr.size(); i++ ){
            qDebug()<<ldr.at(i).fileName();
            // test info method file
            qDebug()<<QString(cfg.methods_path)+QString("/")+
                    QString(ldr.at(i).fileName())+QString("/method.info");
            if(!QFile::exists(QString(cfg.methods_path)+
                    QString("/")+QString(ldr.at(i).fileName())
                    +QString("/method.info"))){
                qDebug()<<"Error"<<ldr.at(i).fileName()<<"not found";
            }else{
                qDebug()<<"OK"<<ldr.at(i).fileName()<<"found";
                // create method object and load from info file.
                Method * met = new Method();
                if(!met->load(QString(cfg.methods_path)+QString("/")+
                              QString(ldr.at(i).fileName()))){
                    qDebug()<<"Error load method";
                    delete met;
                }else{
                    qDebug()<<"OK load method";
                    qDebug()<<"name of method"<<met->name;
                    mdl.append(met);
                }
            }
        }
        qDebug()<<"Size of method list"<<mdl.size();
    }else{
        // methods not found. fixit - show error message
        qDebug()<<"DIR NOT";
    }
    // test splash
    //splash->showMessage("YES");
    //sleep(5);
    Mwin win(&mdl);
    win.show();
    //splash->finish(&win);
    return a.exec();

}

